package com.ios.service.impl;

import com.ios.dao.DisablerLocationMapper;
import com.ios.dao.DisablerUserMapper;
import com.ios.dto.DisablerLocationExecution;
import com.ios.entity.DisablerLocation;
import com.ios.enums.LocationStateEnum;
import com.ios.service.IDisablerLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class DisablerLocationServiceImpl implements IDisablerLocationService{
    @Autowired
    private DisablerLocationMapper disablerLocationMapper;
    @Autowired
    private DisablerUserMapper disablerUserMapper;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public DisablerLocationExecution AddDisablerLocation(int disabler_id, String latitude, String longitude) {
        if (disabler_id==-1||latitude.equals(null)||longitude.equals(null)){
            return new DisablerLocationExecution(LocationStateEnum.NULL_PARAM);
        }else if (!CheckDisablerId(disabler_id)){
            return new DisablerLocationExecution(LocationStateEnum.NULL_USER);
        }else {
            try{
                DisablerLocation disablerLocation = new DisablerLocation();
                disablerLocation.setDisablerId(disabler_id);
                disablerLocation.setDisablerLatitude(latitude);
                disablerLocation.setDisablerLongitude(longitude);
                disablerLocation.setCreateTime(new Date());
                disablerLocationMapper.insert(disablerLocation);
                return new DisablerLocationExecution(LocationStateEnum.SUCCESS_ADD,disablerLocation);
            }catch (Exception e){
                return new DisablerLocationExecution(LocationStateEnum.FAIL_ADD);
            }
        }
    }

    @Override
    public DisablerLocationExecution QueryDisablerLocation(int disabler_id) throws ParseException {
        if (disabler_id==-1){
            return new DisablerLocationExecution(LocationStateEnum.NULL_PARAM);
        }else if (!CheckDisablerId(disabler_id)){
            return new DisablerLocationExecution(LocationStateEnum.NULL_USER);
        }else {
            try{
                int id = 0;
                DisablerLocation newdisablerLocation = new DisablerLocation();
                List<DisablerLocation> disablerLocations = disablerLocationMapper.selectByDisabler(disabler_id);
                for (DisablerLocation disablerLocation:disablerLocations){
                    Date maxdate = dateFormat.parse("2000-1-1");
                    if (disablerLocation.getCreateTime().compareTo(maxdate)>0){
                        maxdate = disablerLocation.getCreateTime();
                        id = disablerLocation.getId();
                    }
                }
                DisablerLocation disablerLocation = disablerLocationMapper.selectById(id);
                return new DisablerLocationExecution(LocationStateEnum.SUCCESS_QUERY,disablerLocation);
            }catch (Exception e){
                return new DisablerLocationExecution(LocationStateEnum.FAIL_QUERY);
            }
        }
    }

    /*判断用户是否存在*/
    private boolean CheckDisablerId(Integer disablerId){
        List<Integer > AllId = disablerUserMapper.getAllDisablerId();
        for (Integer id : AllId){
            if (id==disablerId){
                return true;
            }
        }
        return false;
    }
}
